package cn.java79.quiz.service;

import cn.java79.quiz.dao.AdminMapper;
import cn.java79.quiz.dao.QuestionsMapper;
import cn.java79.quiz.dao.UserMapper;
import cn.java79.quiz.entity.Admin;
import cn.java79.quiz.entity.Questions;
import cn.java79.quiz.entity.User;
import cn.java79.quiz.pojo.AjaxPage;
import cn.java79.quiz.util.LogUtil;
import cn.java79.quiz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class AdminService {
    public static final int PAGESIZE=10;
    public static AdminService ME=new AdminService();
    public AjaxPage<Admin> search(String loginname, String realname, int pageNo){
        return this.search(loginname, realname, pageNo,PAGESIZE);
    }
    public AjaxPage<Admin> search(String loginname, String realname, int pageNo, int pageSize){

        SqlSession s = MybatisUtil.get();
        Page<Admin> page = PageHelper.startPage(pageNo, pageSize);
        try {

            AdminMapper mapper = s.getMapper(AdminMapper.class);
            mapper.search(loginname,realname);
            System.out.println(page.getPageNum()+">>>>>>>>>>>>>>>>>>>>>>>>>>");
            return new AjaxPage<Admin>(page.subList(0, page.size()), page.getTotal(), page.getPageNum(), page.getPageSize(), page.getPages());
        } finally {
            MybatisUtil.close(s);
        }

    }
    public Admin login(String login, String pass){
        SqlSession s = MybatisUtil.get();
        try {
            AdminMapper mapper = s.getMapper(AdminMapper.class);
            return  mapper.login(login,pass);
        }finally {
            MybatisUtil.close(s);
        }
    }


    public int del(Long id) {
        return (int) (Math.random()*2);
    }
}
